const writeData = require("../db/module/writeData");

let saveFileData = async (req, res, next) => {
  try {
    let fileName = decodeURIComponent(req.file.originalname);

    let result = await writeData(
      {
        fileName: req.file.originalname,
        fileSize: (req.file.size / 1024 / 1024).toFixed(2) + "MB",
        fileType: req.file.mimetype,
        filePath: req.file.path,
        fileId: req.file.filename,
        createTime: new Date().toLocaleString(),
      },
      "fileList"
    );
    if (result.code == 20000) {
      next();
    } else {
      res.send({
        code: 50000,
        msg: "图片上传失败，请联系管理员",
      });
    }
  } catch (error) {
    res.send({
      code: 50000,
      msg: "系统异常",
    });
  }
};

module.exports = { saveFileData };
